#ifndef _HMC5883L_H_
#define _HMC5883L_H_

#include "common.h"

#ifdef __cplusplus
extern "C"
{
#endif


//Definition of HMC5883L Register
#define HMC5883_ADDR 0x1E
#define HMC5883_R_CONFA 0
#define HMC5883_R_CONFB 1
#define HMC5883_R_MODE 2
#define HMC5883_R_XM 3
#define HMC5883_R_XL 4
#define HMC5883_R_ZM 5
#define HMC5883_R_ZL 6
#define HMC5883_R_YM 7
#define HMC5883_R_YL 8
#define HMC5883_R_STATUS 9
#define HMC5883_R_IDA 10
#define HMC5883_R_IDB 11
#define HMC5883_R_IDC 12

#define HMC5883_CONTINMODE	0
#define HMC5883_SINGLEMODE	1
#define HMC5883_IDLEMODE	2

#define HMC5883_OUTPUT_0_75_Hz		0x00
#define HMC5883_OUTPUT_1_5_Hz		0x01
#define HMC5883_OUTPUT_3_Hz			0x02
#define HMC5883_OUTPUT_7_5_Hz		0x03
#define HMC5883_OUTPUT_15_Hz		0x04
#define HMC5883_OUTPUT_30_Hz		0x05
#define HMC5883_OUTPUT_75_Hz		0x06

#define HMC5883_1_SAMPLE_AVG		0x00
#define HMC5883_2_SAMPLE_AVG		0x01
#define HMC5883_4_SAMPLE_AVG		0x02
#define HMC5883_8_SAMPLE_AVG		0x03

#define HMC5883_BIAS_NORMAL			0x00	//default
#define HMC5883_BIAS_POSITIVE		0x01
#define HMC5883_BIAS_NEGATIVE		0x02

#define MAG_0P88GA 0x00
#define MAG_1P3GA 0x01
#define MAG_1P9GA 0x02
#define MAG_2P5GA 0x03
#define MAG_4P0GA 0x04
#define MAG_4P7GA 0x05
#define MAG_5P6GA 0x06
#define MAG_8P1GA 0x07

int HMC5883L_READ_REG(GIO_Handle inhd,GIO_Handle outhd, Uint8 REG, Uint8 *buf, size_t len);
int HMC5883L_WRITE_REG(GIO_Handle inhd,GIO_Handle outhd, Uint8 REG,Uint8 value, op_mode op);
int HMC5883_CONNECT(GIO_Handle inhd,GIO_Handle outhd);

int HMC5883_SET_Bias(GIO_Handle inhd,GIO_Handle outhd, Uint8 bias);
int HMC5883_SET_DataOutputRate(GIO_Handle inhd,GIO_Handle outhd, Uint8 rate);
int HMC5883_SET_SampleAvg(GIO_Handle inhd,GIO_Handle outhd, Uint8 num_sample_avg);
int HMC5883_SET_MODE(GIO_Handle inhd,GIO_Handle outhd, Uint8 mode);
int HMC5883_SET_RANGE(GIO_Handle inhd,GIO_Handle outhd, Uint32 range);

int HMC5883_GET_RDATA(GIO_Handle inhd,GIO_Handle outhd,short *data);
void HMC5883_SCALE_DATA(short *rdata, double *data, int mag_scale);
void HMC5883_CAL_DATA(short*rdata, double *data, int mag_scale);	//rdata-raw data, data-output 3*1, mag_scale
double HMC5883_GETYAW(double pitch, double roll, double *mag_data, double declination);

//Register construction
//HMC5883_R_CONFA:
//CRA7	MA1	MA0	DO2	DO1	DO0	MS1	MS0
//CRA7 -- should always be 0;
//MA1 MA0 -- select number of samples averaged(1 ~ 8) per measurement output
//				00 = 1, 01 = 2, 10 = 4, 11 = 8(Default)
//DO2 DO1 DO0 -- data output rate
//					000=0.75Hz, 001=1.5, 010=3, 011=7.5, 100=15(Default), 101=30, 110=75
//MS1 MS0 -- measurement mode
//				00=normal mode(default),01=positive bias,10=negative bias, 11=reserved

//HMC5883_R_CONFB:
//GN2 GN1 GN0 CRB4 CRB3 CRB2 CRB1 CRB0
//CRB4 CRB3 CRB2 CRB1 CRB0 -- should be 0;
//GN2 GN1 GN0 -- gain for device
//				000	0.88Ga	1370LSB/Ga	0xF800-0x07FF(-2048-2047)
//				001	1.3Ga	1090(default)
//				010	1.9Ga	820
//				011	2.5Ga	660
//				100	4.0Ga	440
//				101	4.7Ga	390
//				110	5.6Ga	330
//				111	8.1Ga	230

#ifdef __cplusplus
}
#endif /* extern "C" */


#endif /*HMC5883L_H_*/

